package cn.clwxy.gdz.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.clwxy.gdz.bo.NewOrderByStudent;
import cn.clwxy.gdz.bo.OrderImformation;
import cn.clwxy.gdz.dao.OrderDao;
import cn.clwxy.gdz.dao.OrderDaoImpl;
import cn.clwxy.gdz.dao.UserDao;
import cn.clwxy.gdz.dao.UserDaoImpl;
import cn.clwxy.gdz.entity.Order;
import cn.clwxy.gdz.entity.Student;
import cn.clwxy.gdz.entity.User;
import cn.clwxy.gdz.entity.UserSubjectRelative;
import cn.clwxy.gdz.service.CreatNewOrderByStudentServiceImpl;
import cn.clwxy.gdz.service.CreateNewOrderByStudentService;
import cn.clwxy.gdz.service.LoginService;
import cn.clwxy.gdz.service.LoginServiceImpl;
import cn.clwxy.gdz.util.JsonSerializeHelper;
import cn.clwxy.gdz.util.MD5Encrypter;

import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

@WebServlet(urlPatterns={"/NewOrderByUserServlet"})
public class NewOrderByUserServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
//		����ҳ�����Ϣ
		Integer teacherId =Integer.valueOf(request.getParameter("teacherId"));
		String userPwd=request.getParameter("userName");
		String userPhone=request.getParameter("userPhone");
		userPwd=userPhone+userPwd+userPhone;
		userPwd=MD5Encrypter.encrypt(userPwd);
		LoginService loginService=new LoginServiceImpl();
		Student student=loginService.getStudent(userPhone, userPwd);
		Integer studentId=(Integer)request.getSession().getAttribute("id");
		Order order=new Order();
//		验证是否已经预约过
		OrderDao orderDao1=new OrderDaoImpl();
		int isClick=orderDao1.findOrderBySIdTIdInitiator(studentId, teacherId, 1);
		if(isClick>0){
			System.out.println("已经预约过了");
			order.setOrderId(-1);
			String json=JsonSerializeHelper.JsonSerializer(order);
			response.getWriter().write(json);
			return;
		}
		
		
		
		
		
//		验证学生的手机号和密码
		if(null==student){
			order.setOrderId(0);
			String json=JsonSerializeHelper.JsonSerializer(order);
			response.getWriter().write(json);
		}
		
		
		Integer gradeId=Integer.valueOf(request.getParameter("gradeId"));
		String subjects[]=request.getParameterValues("subjects");
		System.out.println(subjects);
		int subjectLength=0;
		try {
			subjectLength = subjects.length;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			response.getWriter().write("0");
		}
		Integer subjectIds[]=new Integer[subjectLength];
		int i=0;
		for (String subjectName : subjects) {
			subjectIds[i]=Integer.valueOf(subjectName);
			i++;
		}
		
//		װ��
		NewOrderByStudent newOrderByStudent=new NewOrderByStudent();
		newOrderByStudent.setGradeId(gradeId);
		newOrderByStudent.setSubjectIds(subjectIds);
		newOrderByStudent.setTeacherId(teacherId);
		newOrderByStudent.setStudentPwd(userPwd);
		newOrderByStudent.setStudentPhone(userPhone);
		newOrderByStudent.setStudentId(studentId);
		System.out.println(newOrderByStudent);
//		�ϴ�����ݿ�
		CreateNewOrderByStudentService createNewOrderByStudentService=new CreatNewOrderByStudentServiceImpl();
		Integer orderId=createNewOrderByStudentService.createNewOrderByStudent(newOrderByStudent);
		System.out.println("�Ƿ�ɹ���"+orderId);
		OrderDao orderDao=new OrderDaoImpl();
		System.out.println(orderId);
		OrderImformation orderInformation=orderDao.findOderByOrderId(orderId);
		
		if(orderId!=0){
			String json=JsonSerializeHelper.JsonSerializer(orderInformation);
			response.getWriter().write(json);
		}else{
			order.setOrderId(0);
			String json=JsonSerializeHelper.JsonSerializer(order);
			response.getWriter().write(json);
		}
	}
}
